package com.e3.sysmg.config.service.impl;

import java.util.List;

import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;
import com.e3.sysmg.config.dao.ParameterDAO;
import com.e3.sysmg.config.entity.ParameterEntity;
import com.e3.sysmg.config.model.ParameterModel;
import com.e3.sysmg.config.service.ParameterCacheManager;
import com.e3.sysmg.config.service.ParameterService;

public class ParameterServiceImpl extends SuperService implements ParameterService {
	
	private ParameterDAO parameterDAO;
	
	private ParameterDAO getParameterDAO(){
		if(parameterDAO == null){
			parameterDAO = DAOHelper.createDAOInstance(ParameterDAO.class);
		}
		return parameterDAO;
	}

	public ParameterModel newParameter(ParameterModel model) throws ServiceException {
		validateParameter(model);
		checkParameterExisted(model);
		
		ParameterEntity entity = parameterModelToEntity(model, null);
		getParameterDAO().persist(entity);
		
		model.setId(entity.getId());
		
		ParameterCacheManager.INSTANCE.put(model);
		
		return model;
	}

	public ParameterModel modifyParameter(ParameterModel model) throws ServiceException {
		validateParameter(model);

		long id = model.getId();
		ParameterEntity entity = getParameterDAO().getById(ParameterEntity.class, id);
		if (entity == null) {
			return newParameter(model);
		}

		String oldName = entity.getName();
		if (!oldName.equals(model.getName())) {
			checkParameterExisted(model);
		}

		parameterModelToEntity(model, entity);
		getParameterDAO().merge(entity);
		
		ParameterCacheManager.INSTANCE.put(model);
		return model;
	}

	public void deleteParameterByIds(List<Long> ids) throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return;
		}
		
		getParameterDAO().deleteParameterByIds(ids);
		
		ParameterCacheManager.INSTANCE.remove(ids);
	}

	public void checkParameterExisted(ParameterModel model) throws ServiceException {
		if (model == null) {
			return;
		}

		String name = model.getName();
		
		StringBuffer msgs = new StringBuffer();
		int size = (int)getParameterDAO().getParameterSizeByName(name);
		if (size>0) {
			msgs.append("名称为'" + name + "' ");
		}
		if(msgs.length()>0){
			msgs.append("已经存在，请设置新的名称。");
			throw new ServiceException(msgs.toString());
		}
	}

	private void validateParameter(ParameterModel model) throws ServiceException{
		String name = model.getName();

		if ( name == null || name.trim().length() == 0) {
			throw new ServiceException("请设置编码,名称和符号。");
		}
	}
	
	private ParameterEntity parameterModelToEntity(ParameterModel model,ParameterEntity entity){
		entity = ocModelToEntity(ParameterEntity.class, model, entity);
		entity.setName(model.getName());
		entity.setType(model.getType());
		entity.setLevel(model.getLevel());
		entity.setDataType(model.getDataType());
		entity.setValue(model.getValue());
		entity.setRemark(model.getRemark());
		return entity;

	}
}
